﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

using CE.Database;
using CE.Global;
using CE.HR;
using CE.Job;

public partial class HR_job_post : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.HRId] == null)
        {
            Response.Redirect("login.aspx");
        }

        if (!IsPostBack)
        {
            GetResultTable(hidJobTitle.Value, hidCreatedBy.Value, hidStartDate.Value, hidEndDate.Value, hidStatus.Value);
        }
    }
    private void GetResultTable(string searhjobtitle, string createdby, string startdate, string enddate, string status)
    {
        string _startdate = "01/01/1900";
        string _enddate = "01/01/2900";
        if (startdate != "")
            _startdate = startdate;
        if (enddate != "")
            _enddate = enddate;

        HRAccountDB _accountdb = new HRAccountDB();
        HRAccount _account = _accountdb.GetAccount(Int64.Parse(Session[CE.Global.General.HRId].ToString()));

        MyDatabase _db = new MyDatabase();
        DataSet _dsResult = new DataSet();
        string _spName = "[sprocJobSelectSingleItemListByCountry]";
        SortedList _spara = new SortedList();
        _spara.Add("@Country", _account.Country);
        _spara.Add("@Sort", "CreatedDate");
        _spara.Add("@SortBy", "desc");
        _spara.Add("@JobTitle", searhjobtitle);
        _spara.Add("@CreatedBy", createdby);
        _spara.Add("@StartDate", DateTime.ParseExact(_startdate, "dd/MM/yyyy", null));
        _spara.Add("@EndDate", DateTime.ParseExact(_enddate, "dd/MM/yyyy", null));
        _spara.Add("@Status", bool.Parse(status));
        _dsResult = _db.GetDataSet(_spName, _spara);
        
        PagedDataSource pageds = new PagedDataSource();
        pageds.DataSource = _dsResult.Tables[0].DefaultView;
        pageds.AllowPaging = true;
        pageds.PageSize = 30;

        int curpage = 0;

        if (Request.QueryString["page"] != null)
        {
            curpage = Convert.ToInt32(Request.QueryString["page"]);
        }
        else
        {
            curpage = 1;
        }

        pageds.CurrentPageIndex = curpage - 1;

        if (pageds.Count > 0)
            lblCurrpage.Text = "";
        if (curpage == 1 && pageds.DataSourceCount > pageds.PageSize)
        {
            lblCurrpage.Text = "Pages: 1";
            lblCurrpageTop.Text = "Pages: 1";
        }
        else if (pageds.DataSourceCount == 0)
        {
            lblCurrpage.Text = "No data to display.";
            lblCurrpageTop.Text = "";
        }
        else if (curpage > 1 && pageds.DataSourceCount > pageds.PageSize)
        {
            lblCurrpage.Text = "Pages: <a href='job-post.aspx?" /*+ "&sortby=" + ddlSort.SelectedItem.Text*/ + "&page=1'>1</a>";
            lblCurrpageTop.Text = "Pages: <a href='job-post.aspx?" /*+ "&sortby=" + ddlSort.SelectedItem.Text*/ + "&page=1'>1</a>";
        }
        for (int i = 2; i <= pageds.PageCount; i++)
        {
            if (i == curpage)
            {
                lblCurrpage.Text = lblCurrpage.Text + " " + i.ToString();
                lblCurrpageTop.Text = lblCurrpageTop.Text + " " + i.ToString();
            }
            else
            {
                lblCurrpage.Text = lblCurrpage.Text + " <a href='job-post.aspx?page=" + i.ToString() /*+ "&sortby=" + ddlSort.SelectedItem.Text*/ + "'>" + i.ToString() + "</a>";
                lblCurrpageTop.Text = lblCurrpageTop.Text + " <a href='job-post.aspx?page=" + i.ToString() /*+ "&sortby=" + ddlSort.SelectedItem.Text*/ + "'>" + i.ToString() + "</a>";
            }
        }

        gvJob.DataSource = pageds;
        gvJob.DataBind();
    }
    protected void gvJob_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DeleteJob")
        {
            int _jobid = int.Parse(e.CommandArgument.ToString());
            Int64 _hrid = Int64.Parse(Session[CE.Global.General.HRId].ToString());

            JobDB _jobdb = new JobDB();
            Int64 result = _jobdb.DeleteJobByID(_jobid, _hrid);
            if (result > 0)
            {
                GetResultTable(hidJobTitle.Value, hidCreatedBy.Value, hidStartDate.Value, hidEndDate.Value, hidStatus.Value);
            }
        }
    }
    protected void gvJob_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblStatus = (Label)e.Row.FindControl("lblStatus");
            Label lblCreatedBy = (Label)e.Row.FindControl("lblCreatedBy");

            if (lblStatus.Text == "True")
                lblStatus.Text = "Visible";
            else
                lblStatus.Text = "Hidden";

            Int64 _hrid = Int64.Parse(lblCreatedBy.Text);
            HRAccountDB _accountdb = new HRAccountDB();
            HRAccount _account = _accountdb.GetAccount(_hrid);

            if (_account != null)
            {
                lblCreatedBy.Text = _account.Name;
            }
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        hidJobTitle.Value = txtJobTitle.Text.Trim();
        hidCreatedBy.Value = txtCreatedBy.Text.Trim();
        hidStartDate.Value = txtStartDate.Text.Trim();
        hidEndDate.Value = txtEndDate.Text.Trim();
        hidStatus.Value = ddlStatus.SelectedValue;
        GetResultTable(hidJobTitle.Value, hidCreatedBy.Value, hidStartDate.Value, hidEndDate.Value, hidStatus.Value);
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtJobTitle.Text = "";
        txtCreatedBy.Text = "";
        txtStartDate.Text = "";
        txtEndDate.Text = "";
        ddlStatus.SelectedIndex = 0;
        hidJobTitle.Value = "";
        hidCreatedBy.Value = "";
        hidStartDate.Value = "";
        hidEndDate.Value = "";
        hidStatus.Value = "true";
        GetResultTable(hidJobTitle.Value, hidCreatedBy.Value, hidStartDate.Value, hidEndDate.Value, hidStatus.Value);
    }
}
